package com.sinodata.bsm.center.web.action.event;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.EventBean;
import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UserFunctionValidate;
import com.sinodata.bsm.center.service.event.EventService;
import com.sinodata.bsm.center.user.UserUtil;
import com.sinodata.bsm.center.web.action.BaseExportAction;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.vo.Event;

/**
 * <p>
 * Description: 活动事件导出
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-29 PM 6:07:48     dongyushi      1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class EventExportAction extends BaseExportAction<Event> {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 6387886204744013367L;

    @Autowired
    private EventService eventService;

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseExportAction#doExport()
     */
    @Override
    public String doExport() throws Exception {
        String fileName = "列表事件";
        String displayFileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
        response.setContentType("APPLICATION/msexcel; charset=utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=\"" + displayFileName.trim().replaceAll("\\s", "") + ".xls\"");

        HSSFSheet sheet = workBook.createSheet("列表事件");
        //设置列的宽度  
        sheet.setColumnWidth(0, 8000);
        sheet.setColumnWidth(1, 8000);
        sheet.setColumnWidth(2, 4000);
        sheet.setColumnWidth(3, 20000);
        sheet.setColumnWidth(4, 5000);
        sheet.setColumnWidth(5, 4000);
        sheet.setColumnWidth(6, 5000);
        sheet.setColumnWidth(7, 4000);
        sheet.setColumnWidth(8, 5000);

        //表头
        HSSFCellStyle style = getStyle(STYLE_HEADING);
        HSSFRow rowHead = sheet.createRow(0);

        HSSFCell resNameHead = rowHead.createCell(0);
        resNameHead.setCellValue(new HSSFRichTextString("资源名称"));
        resNameHead.setCellStyle(style);

        HSSFCell eventTypeNameHead = rowHead.createCell(1);
        eventTypeNameHead.setCellValue(new HSSFRichTextString("事件类别"));
        eventTypeNameHead.setCellStyle(style);
        HSSFCell msgHead = rowHead.createCell(2);
        msgHead.setCellValue(new HSSFRichTextString("事件级别"));
        msgHead.setCellStyle(style);
        HSSFCell levelNameHead = rowHead.createCell(3);
        levelNameHead.setCellValue(new HSSFRichTextString("事件内容"));
        levelNameHead.setCellStyle(style);
        HSSFCell occurTimeHead = rowHead.createCell(4);
        occurTimeHead.setCellValue(new HSSFRichTextString("产生时间"));
        occurTimeHead.setCellStyle(style);
        HSSFCell ackHead = rowHead.createCell(5);
        ackHead.setCellValue(new HSSFRichTextString("认领人"));
        ackHead.setCellStyle(style);
        HSSFCell ackTimeHead = rowHead.createCell(6);
        ackTimeHead.setCellValue(new HSSFRichTextString("认领时间"));
        ackTimeHead.setCellStyle(style);
        HSSFCell countHead = rowHead.createCell(7);
        countHead.setCellValue(new HSSFRichTextString("检测次数"));
        countHead.setCellStyle(style);
        HSSFCell lastOccurTimeHead = rowHead.createCell(8);
        lastOccurTimeHead.setCellValue(new HSSFRichTextString("最后刷新时间"));
        lastOccurTimeHead.setCellStyle(style);

        Condition condition = new Condition();
        Map<String, String> map = new HashMap<String, String>();
        String level = request.getParameter("level");
        String status = request.getParameter("status");
        String resId = request.getParameter("resId");
        //当前用户可见资源范围
        List<Long> authScopeList = UserFunctionValidate.getResIdListForUser(FunctionConstants.VIEW_RES);
        StringBuffer resIds = new StringBuffer();
        if (authScopeList != null && authScopeList.size() != 0) {
            for (Long resId1 : authScopeList) {
                resIds.append(resId1).append(",");
            }
            resIds.delete(resIds.lastIndexOf(","), resIds.length());
        }
        if (resId == null || resId.equals("")) {
            if (resIds.toString() != null && !resIds.toString().equals("")) {
                String userId = UserUtil.getInstance().getCurrentUserId();
                if (!UserUtil.getInstance().isSysAdmin(userId) && !UserUtil.getInstance().isGuest(userId)) {//guest用户有全部数据的查看权限
                    map.put("resIds", resIds.toString());
                }
            } else {
                workBook.write(response.getOutputStream());
                return null;
            }
        } else {
            map.put("resIds", resId);
        }

        if (level != null && !level.equals("")) {
            map.put("level_", level);
        }
        if (status != null && !status.equals("")) {
            map.put("status", status);
        } else {
            map.put("status", EventConstants.STATUS_NEW + "," + EventConstants.STATUS_ACKNOWLEDGED);
        }
        condition.setWhere(map);

        List<EventBean> list = eventService.findEventByCondition(condition, "");
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simple = new SimpleDateFormat(format);
        //表体赋值
        style = getStyle(STYLE_CONTENT);
        int rowNum = 1;
        for (EventBean eventBean : list) {
            HSSFRow row = sheet.createRow(rowNum++);

            HSSFCell resName = row.createCell(0);
            resName.setCellValue(new HSSFRichTextString(eventBean.getResName()));

            HSSFCell eventTypeName = row.createCell(1);
            eventTypeName.setCellValue(new HSSFRichTextString(eventBean.getEventTypeName()));
            HSSFCell levelName = row.createCell(2);
            levelName.setCellValue(new HSSFRichTextString(EventConstants.getLevelDesc(eventBean.getEvent().getLevel())));
            HSSFCell msg = row.createCell(3);
            msg.setCellValue(new HSSFRichTextString(eventBean.getEvent().getMsg()));
            HSSFCell occurTime = row.createCell(4);
            occurTime.setCellValue(new HSSFRichTextString(eventBean.getOccurTime()));
            HSSFCell ack = row.createCell(5);
            ack.setCellValue(new HSSFRichTextString(eventBean.getEvent().getAckUser() == null ? "" : (UserUtil.getInstance().getUserById(eventBean.getEvent().getAckUser()) == null ? eventBean.getEvent().getAckUser() + "此用户已删除" : UserUtil.getInstance().getUserById(eventBean.getEvent().getAckUser()).getFullName())));
            HSSFCell ackTime = row.createCell(6);
            ackTime.setCellValue(new HSSFRichTextString(eventBean.getEvent().getAckTime() == null ? "" : simple.format(eventBean.getEvent().getAckTime())));
            HSSFCell count = row.createCell(7);
            count.setCellValue(eventBean.getEvent().getCount() == null ? new HSSFRichTextString("") : new HSSFRichTextString(String.valueOf(eventBean.getEvent().getCount())));
            HSSFCell lastOccurTime = row.createCell(8);
            lastOccurTime.setCellValue(new HSSFRichTextString((eventBean.getLastOccurTime() == null) ? "" : eventBean.getLastOccurTime()));
            if (rowNum % 2 != 0) {
                resName.setCellStyle(style);
                eventTypeName.setCellStyle(style);
                msg.setCellStyle(style);
                levelName.setCellStyle(style);
                occurTime.setCellStyle(style);
                ack.setCellStyle(style);
                ackTime.setCellStyle(style);
                count.setCellStyle(style);
                lastOccurTime.setCellStyle(style);
            }
        }
        workBook.write(response.getOutputStream());
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#input()
     */
    @Override
    public String input() throws Exception {
        return null;
    }

}